package java44th.group.controller.wx.cart;

import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.po.MarketUser;
import java44th.group.bean.wx.bo.cart.*;
import java44th.group.bean.wx.vo.cart.CartCheckoutVo;
import java44th.group.bean.wx.vo.cart.CartIndexVO;
import java44th.group.service.wx.cart.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author 小焦
 * @since 2022/10/14 10:28
 */

@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    WxCartService cartService;

    /**
     * 1. 获取请求参数--{goodsId: 1181087, number: 3, productId: 366}
     * 2. 处理业务逻辑--如果该商品已加入购物车,则修改该商品数量返回所有商品数量,若不存在该商品在cart数据库新增一条记录
     * 3. 返回响应--{"errno":0,"data":4,"errmsg":"成功"}
     *
     * @param cartAddBO
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/14 上午 11:14
     */
    @PostMapping("add")
    public BaseRespVo add(@RequestBody CartAddBO cartAddBO) {
        Integer count = cartService.add(cartAddBO,this.getuserId());
        return BaseRespVo.ok(count);
    }


    @GetMapping("index")
    public BaseRespVo index() {

        CartIndexVO cartIndexVO = cartService.index(this.getuserId());
        return BaseRespVo.ok(cartIndexVO);
    }

    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody CartAddBO fastaddBo) {
        int code = cartService.fastadd(fastaddBo,this.getuserId());
        return BaseRespVo.ok(code);
    }

    /**
     * 1. 获取请求参数--{productIds: [361], isChecked: 0}
     * 2. 处理业务逻辑--修改购物车中订单的选中状态,返回新的数据
     * 3. 返回响应--
     * @param checkedBo
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/14 下午 10:55
     */
    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedBo checkedBo) {
        CartIndexVO cartcheckedVo= cartService.checked(checkedBo,this.getuserId());
        return BaseRespVo.ok(cartcheckedVo);
    }


    /**
     * 1. 获取请求参数--{productId: 361, goodsId: 1181082, number: 3, id: 647}
     * 2. 处理业务逻辑--修改该商品的number
     * 3. 返回响应--{"errno":0,"errmsg":"成功"}
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/15 上午 10:12
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody CartUpdateBo updateBo){
    int code=cartService.update(updateBo);
    if(code!=1){
        return BaseRespVo.invalidParameter("操作失败");
    }
    return BaseRespVo.ok();
    }

    /**
     * 1. 获取请求参数--{productIds: [179]}
     * 2. 处理业务逻辑--逻辑删除购物车中的该条记录
     * 3. 返回响应--重新响应购物车页面
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/15 上午 10:15
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody ProductIdsBO productIds){
        CartIndexVO indexVO =cartService.delete(productIds,this.getuserId());
        return BaseRespVo.ok(indexVO);
    }

    /**
     * 1. 获取请求参数--cartId=0&addressId=0&couponId=-1&userCouponId=-1&grouponRulesId=0
     * 2. 处理业务逻辑--下单页面回显
     * 3. 返回响应--
     * @param checkoutBo
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/15 下午 02:52
     */
    @GetMapping("checkout")
    public BaseRespVo checkout(CartCheckoutBo checkoutBo){
        CartCheckoutVo checkoutVo =cartService.checkout(checkoutBo,this.getuserId());
        return BaseRespVo.ok(checkoutVo);
    }

    /**
     * 获取购物车商品数量
     * 返回{"errno":0,"data":6,"errmsg":"成功"}
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/15 下午 02:46
     */
    @GetMapping("goodscount")
    public BaseRespVo goodscount(){
    long count=cartService.goodscount(this.getuserId());
    return BaseRespVo.ok(count);
    }

    /**
     *  获取userId的方法
     * @return java.lang.Integer
     * @author 小焦
     * @since 2022/10/15 下午 02:06
     */
    public Integer getuserId(){
        Subject subject = SecurityUtils.getSubject();

        if(subject.getPrincipals()==null){
           return 0;
        }
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        Integer id = primaryPrincipal.getId();
        return id;
    }
}
